package com.zhiliang.waibao.myapplication.ui;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.TextView;
import com.zhiliang.waibao.myapplication.R;
import com.zhiliang.waibao.myapplication.component.ApplicationComponent;
import com.zhiliang.waibao.myapplication.ui.base.BaseFragment;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class CFragment extends BaseFragment {

    @BindView(R.id.button_c)
    Button buttonC;
    Unbinder unbinder;
    @BindView(R.id.viewstub_test)
    ViewStub viewstubTest;
    private TextView hint_text;

    public static CFragment newInstance() {

        Bundle args = new Bundle();

        CFragment fragment = new CFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentLayout() {
        return R.layout.module_fragment_base;
    }

    @Override
    public void initInjector(ApplicationComponent appComponent) {

    }

    @Override
    public void bindView(View view, Bundle savedInstanceState) {

    }

    @Override
    public void initData() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.button_c)
    public void onViewClicked() {

        //inflate 方法只能被调用一次，因为调用后viewStub对象就被移除了视图树；
        // 所以，如果此时再次点击显示按钮，就会崩溃，错误信息：ViewStub must have a non-null ViewGroup viewParent；
        // 所以使用try catch ,当此处发现exception 的时候，在catch中使用setVisibility()重新显示
        try {
            View view = viewstubTest.inflate();
            hint_text = view.findViewById(R.id.view_stub_inflateid);
            viewstubTest.setVisibility(View.VISIBLE);
        } catch (Exception e) {
            viewstubTest.setVisibility(View.VISIBLE);
        } finally {
            hint_text.setText("没有相关数据，请刷新");
        }

    }
}
